package action;

import Impl.UserDaoImpl;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ChangeInfo")
public class ChangeInfo extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("--------正在执行ChangeInfo---------");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        boolean sex;
        int id = Integer.parseInt(request.getParameter("id"));
        if (method != null && method.equals("edit")) {
            HttpSession session =request.getSession();
            User user = (User) session.getAttribute("user");
//            System.out.println(user);
            String name = request.getParameter("uname");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            String school = request.getParameter("school");
            String grade = request.getParameter("grade");
            String real_name = request.getParameter("real_name");
            String pic = request.getParameter("face");
            String sex_tmp =request.getParameter("sex");
//            System.out.println(name);
            if (name==null){
                name=user.getName();
            }
            if (password.equals("")){
                password=user.getPassword();
            }
            if(email.equals("")){
                email=user.getEmail();
            }
            if(phone.equals("")){
                phone=user.getPhone();
            }
            if (school==null){
                school=user.getSchool();
            }
            if(grade==null){
                grade=user.getSchool();
            }
//            System.out.println("real_name");
            if(real_name==null){
                real_name=user.getReal_name();
            }

            if(sex_tmp.equals("男")){
                sex = true;
            }else if (sex_tmp.equals("女")){
                sex=false;
            }else{
                sex=user.getSex();
            }
            System.out.println("-------我开始存数据啦----------");
            user.setSex(sex);
            user.setReal_name(real_name);
            user.setId(id);
            user.setGrade(grade);
            user.setSchool(school);
            user.setName(name);
            user.setPassword(password);
            user.setEmail(email);
            user.setPhone(phone);
            System.out.println("电话号码："+user.getPhone());
            user.setImg_path(pic);
            UserDaoImpl userDao = new UserDaoImpl();
            userDao.update(user);
            System.out.println("----------------我存完了！----------------");
            request.getSession().setAttribute("user", user);
            request.getSession().setAttribute("uname", user.getName());
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
